package priv.hxm.speakandlisten;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javafx.scene.image.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import priv.mill.net.MyHttp;

import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class Listen {

	/***
	 * 这里需要改成你自己的实际appid
	 */
	public SpeechRecognizer recognizer;

	public StringBuilder finalString;

	/*
	 * 设计成单例
	 */
	private Listen() {

	}

	static class inner {
		static Listen mk2 = new Listen();
	}

	public static Listen getInstance() {
		return inner.mk2;
	}

	/***
	 * 初始化声音组件
	 * 
	 * @param lblNewLabel
	 */

	public void listen(JLabel lblNewLabel) {

		SpeechUtility.createUtility(SpeechConstant.APPID + "=59bc97bb");//
		recognizer = SpeechRecognizer.createRecognizer();
		recognizer = SpeechRecognizer.getRecognizer();
		recognizer.setParameter(SpeechConstant.DOMAIN, "iat"); // domain:域名
		recognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
		recognizer.setParameter(SpeechConstant.ACCENT, "mandarin");
		recognizer.setParameter(SpeechConstant.ASR_PTT, "0");// 是否带标点符号
		recognizer.setParameter(SpeechConstant.VAD_EOS, "900");// 静音700毫秒结束录音

		// 开始监听语音输入
		recognizer.startListening(new RecognizerListener() {

			@Override
			public void onVolumeChanged(int i) {
				int j = 0;
				if (0<=i&&i<=1)
					j=1;
				else if(2<=i&&i<=4)
					j=2;
				else if(i==5)
					j=3;
				else if(6<=i&&i<=8)
					j=4;
				else if(i==9)
					j=5;
				else if(10<=i&&i<=11)
					j=6;
				else if(i==12)
					j=7;
				else if(i==13)
					j=8;
				else if(i==14)
					j=9;
				else if(i==15)
					j=10;
				else 
					j=11;
					lblNewLabel.setIcon(new ImageIcon("res/" + j + "voic.png"));				
			}

			@Override
			public void onResult(RecognizerResult result, boolean arg1) {
				if (!arg1) {// 判断是否是最后一次会话结果
					String resultString = result.getResultString();
					finalString = finalString(resultString);
					System.out.println(new String(finalString));
				}
			}

			@Override
			public void onEvent(int arg0, int arg1, int arg2, String arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(SpeechError arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onEndOfSpeech() {
				System.out.println("结束录音");
				lblNewLabel.setVisible(false);
			}

			@Override
			public void onBeginOfSpeech() {
				lblNewLabel.setVisible(true);
				System.out.println("开始录音");
			}
		});

	}

	/*
	 * 正则匹配 合成字符串
	 */
	public StringBuilder finalString(String string) {

		StringBuilder stringBuilder = new StringBuilder();
		Pattern compile = Pattern.compile("\"w\":\"([\\W\\w]{1,5})\"");
		Matcher matcher = compile.matcher(string);
		while (matcher.find()) {
			stringBuilder.append(matcher.group(1));

		}
		return stringBuilder;
	}
}
